package com.along.dev.rbac.service;

/*
 * @Author: liujinyi
 * @DateTime: 2025/4/9 17:00
 */
import com.along.dev.rbac.mapper.RoleMapper;
import com.along.dev.rbac.dto.RoleDTO;
import com.along.dev.rbac.entity.Role;
import com.along.dev.rbac.qo.RoleQO;
import com.along.utility.common.utils.BeanUtils;
import com.along.utility.common.utils.JsonUtils;
import com.along.utility.common.utils.ListCopier;
import com.along.utility.page.mybatis.Paging;
import com.github.pagehelper.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
@Service
public class RoleService {

    @Autowired
    private RoleMapper roleMapper;

    private static final Logger logger = LoggerFactory.getLogger(DeptService.class);



    @Paging
    public Page<RoleDTO> find(RoleQO roleQO, Integer page, Integer rows) {
        return ListCopier.pageCopy(this.roleMapper.find(roleQO), RoleDTO.class);
    }

    @Transactional(rollbackFor = Exception.class)
    public void save(RoleDTO roleDTO) {
        Date date = new Date();
        roleDTO.setCreateTime(date);
        roleDTO.setUpdateTime(date);
        Role role = BeanUtils.copy(roleDTO, new Role());
        int size = roleMapper.save(role);
        logger.info("保存部门信息: {}, result: {}", JsonUtils.write(roleDTO), size);
    }
    @Transactional(rollbackFor = Exception.class)
    public void update(RoleDTO roleDTO) {
        roleDTO.setUpdateTime(new Date());
        Role role = BeanUtils.copy(roleDTO, new Role());
        int size = roleMapper.update(role);
        logger.info("修改部门信息: {},result: {}", JsonUtils.write(roleDTO), size);
    }

    public void delete(RoleDTO roleDTO) {
        int result = roleMapper.delete(roleDTO.getRoleId());
        logger.info("删除部门信息：deptId: {}, result: {}", JsonUtils.write(roleDTO.getRoleId()), result);
    }
}
